package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@CrossOrigin
@Slf4j
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    //分页查询
    @GetMapping("")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            log.info("------PermissionController------findPage------前端传过来的queryPageBean------" + queryPageBean);
            PageResult pageResult = permissionService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            log.info("------PermissionController------findPage------查询到的权限信息pageResult------" + pageResult);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            log.info("------PermissionController------findById------前端传过来的权限id------" + id);
            Permission permission = permissionService.findById(id);
            log.info("------PermissionController------findById------通过id查询到的权限信息permission------" + permission);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


    //新增权限
    @PostMapping("")
    public Result add(@RequestBody Permission permission) {
        try {
            log.info("------PermissionController------add------前端新增的权限信息permission------" + permission);
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    //根据id删除权限
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            permissionService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    //更新权限信息
    @PutMapping("")
    public Result update(@RequestBody Permission permission) {
        try {
            log.info("------PermissionController------update-----更新后的数据permission------" + permission);
            permissionService.update(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }


    /**
     * 查询权限所有
     * @return
     */
    @GetMapping(path = "/all")
    public Result findAll() {

        try {
            List<Permission> Permissions = permissionService.findAll();
            log.info("获取当前权限成功",Permissions);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,Permissions);

        } catch (Exception e) {
            log.error("查询所有权限失败！",e);
            return new Result(true, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


}
